package com.wu.yygh.hosp.controller.api;

import com.wu.yygh.common.exception.YyghException;
import com.wu.yygh.common.helper.HttpRequestHelper;
import com.wu.yygh.common.result.Result;
import com.wu.yygh.common.result.ResultCodeEnum;
import com.wu.yygh.common.utils.MD5;
import com.wu.yygh.hosp.service.DepartmentService;
import com.wu.yygh.hosp.service.HospitalService;
import com.wu.yygh.hosp.service.HospitalSetService;
import com.wu.yygh.hosp.service.ScheduleService;
import com.wu.yygh.model.hosp.Department;
import com.wu.yygh.model.hosp.Hospital;
import com.wu.yygh.model.hosp.Schedule;
import com.wu.yygh.vo.hosp.DepartmentQueryVo;
import com.wu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName ApiController
 * @Author 浮生光
 * @Date 2022/7/16 0:48
 * @Description ApiController
 * @Version 1.0
 */
/**
 * 这个类是给模拟医院系统调用的接口，通过http方式调用，但这里只有医院信息，排班信息，科室信息，
 * 没有医院等级信息，他在另外一个模块，如果当前模块需要获取到信息，可以通过nacos注册服务进行调用
 */

@RestController
@RequestMapping("/api/hosp")
public class ApiController {


    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;


    private void verifySignKey(Map<String, Object> map) {
        // 2.判断当前医院是否有对应的签名密钥，由于传过来的签名密钥是MD5的，这里要获取数据库的密钥进行相同摘要进行比对
        String sign = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);

        // 3.判断是否相同签名密钥 如果不对直接抛出异常
        if (!MD5.encrypt(signKey).equals(sign)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    /**
     * 保存或修改数据，根据hoscode去查询数据
     * @param request
     * @return
     */
    @ApiOperation("保存医院信息")
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        // 1.获取请求参数中的医院信息
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
//        verifySignKey(map);

//        // 4.医院信息的logo图片在传输过程进行了base64编码，这里会把“+”变成了“ ”，在保存的时候把它转回去，不然显示不出来
//        String logoData = (String) map.get("logoData");
//        logoData = logoData.replaceAll(" ", "+");
//        map.put("logoData", logoData);

        String hospSign = (String)map.get("sign");
        String hoscode = (String)map.get("hoscode");

        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 4.医院信息的logo图片在传输过程进行了base64编码，这里会把“+”变成了“ ”，在保存的时候把它转回去，不然显示不出来
        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        map.put("logoData", logoData);


        // save 这里没有用MP封装好的方法，参数需要转换以及保存位置不同
        hospitalService.save(map);
        return Result.ok();
    }




    @ApiOperation("查询医院信息")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        // 1.获取请求参数中的医院信息
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        // 2.判断当前医院是否有对应的签名密钥，由于传过来的签名密钥是MD5的，这里要获取数据库的密钥进行相同摘要进行比对
        String hoscode = (String) map.get("hoscode");
        verifySignKey(map);

        // 4.根据医院编号查询医院信息
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }


    @ApiOperation("保存医院科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 1.获取请求参数中的医院信息
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        // 2.判断当前医院是否有对应的签名密钥，由于传过来的签名密钥是MD5的，这里要获取数据库的密钥进行相同摘要进行比对
        verifySignKey(map);

        // 4.调用保存接口
        departmentService.save(map);
        return Result.ok();
    }

    /**
     * 分页查询部门信息
     */
    @ApiOperation("查询医院科室")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {

        // 1.获取参数
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        // 2.签名校验
        String hoscode = (String) map.get("hoscode");
        verifySignKey(map);

        // 3.根据医院编号查询
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String)map.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);

        return Result.ok(pageModel);
    }


    @ApiOperation("保存排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 1.获取参数
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        // 2.签名校验
        verifySignKey(map);

        scheduleService.save(map);
        return Result.ok();
    }


    @ApiOperation("查询排班信息")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //科室编号
        String depcode = (String)paramMap.get("depcode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));
        //TODO 签名校验

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }


//    @PostMapping("/schedule/list")
//    public Result listSchedule(HttpServletRequest request) {
//
//        // 1.获取参数
//        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
//        // 2.签名校验
//        verifySignKey(map);
//
//        // 3.根据医院编号分页查询出其下科室排班信息
//        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
//        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String)map.get("limit"));
//        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
//        scheduleQueryVo.setHoscode((String) map.get("hoscode"));
//        Page<Schedule> schedules =  scheduleService.page(page, limit, scheduleQueryVo);
//        return Result.ok(schedules);
//
//    }

    @ApiOperation("删除医院排班")
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {

        // 1.获取参数
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        verifySignKey(map);
        String hosScheduleId = (String) map.get("hosScheduleId");
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
